package com.guowei.web.domain;

import com.guowei.web.util.DecimalUtil;

import java.math.BigDecimal;

public class ByteUnit {

    private String size;

    private String unit;

    public ByteUnit(long length) {
        if (length >= (long) 1 << 10 && length < (long) 1 << 20) {
            size = DecimalUtil.trim0(DecimalUtil.divide(length, (long) 1 << 10, 2, BigDecimal.ROUND_HALF_UP));
            unit = "KB";
        } else if (length >= (long) 1 << 20 && length < (long) 1 << 30) {
            size = DecimalUtil.trim0(DecimalUtil.divide(length, (long) 1 << 20, 2, BigDecimal.ROUND_HALF_UP));
            unit = "MB";
        } else if (length >= (long) 1 << 30 && length < (long) 1 << 40) {
            size = DecimalUtil.trim0(DecimalUtil.divide(length, (long) 1 << 30, 2, BigDecimal.ROUND_HALF_UP));
            unit = "GB";
        } else if (length >= (long) 1 << 40 && length < (long) 1 << 50) {
            size = DecimalUtil.trim0(DecimalUtil.divide(length, (long) 1 << 40, 2, BigDecimal.ROUND_HALF_UP));
            unit = "TB";
        } else {
            size = String.valueOf(length);
            unit = "B";
        }
    }

    public String getSize() {
        return size;
    }

    public String getUnit() {
        return unit;
    }
}
